package cn.xing.managementSystem.controller;

import cn.xing.managementSystem.bean.ResponseContent;
import cn.xing.managementSystem.bean.pageable.PageableRequest;
import cn.xing.managementSystem.bean.pageable.PageableResponse;
import cn.xing.managementSystem.domain.Course;
import cn.xing.managementSystem.domain.Information;
import cn.xing.managementSystem.enums.CourseType;
import cn.xing.managementSystem.enums.InformationType;
import cn.xing.managementSystem.service.CourseService;
import cn.xing.managementSystem.service.InformationService;
import cn.xing.managementSystem.utils.DateUtils;
import cn.xing.managementSystem.utils.Preconditions;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.List;

/**
 * @author cuixing
 * @package cn.xing.managementSystem.controller
 * @class InformationController
 * @email xing.cui@magicwindow.cn
 * @date 2018/4/25 上午11:04
 * @description
 */

@Controller
public class InformationController {

    @Autowired
    private InformationService informationService;

    @Value("${image.boot.path}")
    private String imageBootPath;

    @Value("${image.url}")
    private String imageUrl;

    @RequestMapping("/informationList")
    public String informationList(){
        return "content/informationList";
    }

    @RequestMapping("/informationPageItems")
    @ResponseBody
    public ModelAndView coursePageItems(PageableRequest pageableRequest){
        ModelAndView mv = new ModelAndView("content/informationItems");
        PageableResponse<Information> pageableResponse = informationService.listPageable(pageableRequest);
        mv.addObject("pageableResponse", pageableResponse);
        return mv;
    }

    @RequestMapping("/editInformation")
    public ModelAndView editCourse(Long informationId){
        Information information = null;
        ModelAndView mv = new ModelAndView();
        if(Preconditions.isNotBlank(informationId)){
            information = informationService.findById(informationId);
        }else{
            information = new Information();
        }
        mv.addObject("information", information);
        mv.setViewName("content/editInformation");
        return mv;
    }


    @RequestMapping("/saveInformation")
    @ResponseBody
    public ResponseContent saveCourse(Long informationId, String title, InformationType informationType, String describe, String informationDetail,
                                      Long informationImageId, @RequestParam("informationImageFile") MultipartFile informationImageFile) throws IOException {
        Information information;
        if(Preconditions.isNotBlank(informationId)){
            information = informationService.findById(informationId);
        }else{
            information = new Information();
        }
        information.setTitle(title);
        information.setInformationType(informationType);
        information.setDescribe(describe);
        information.setInformationDetail(informationDetail);
        if(informationService.saveInformationAndImageStore(information, informationImageId, informationImageFile)){
            return ResponseContent.buildSuccess("保存成功");
        }
        return ResponseContent.buildFail("保存失败");
    }


    @RequestMapping("/deleteInformationById")
    @ResponseBody
    public ResponseContent deleteCourseById(Long informationId) {
        Information information = informationService.findById(informationId);
        information.setDeleted(true);
        information = informationService.saveInformation(information);
        if(Preconditions.isNotBlank(information)){
            return ResponseContent.buildSuccess("删除成功");
        }
        return ResponseContent.buildFail("删除失败");
    }

    @RequestMapping("/v1/getInformationList")
    @ResponseBody
    public ResponseContent<JSONObject> getInformationList() {
        List<Information> zxList = informationService.getInformationListByType(InformationType.ZX);
        List<Information> kchdList = informationService.getInformationListByType(InformationType.KCHD);

        JSONObject result = new JSONObject();
        JSONArray zxArray = new JSONArray();
        JSONArray kchdArray = new JSONArray();

        if(Preconditions.isNotBlank(zxList)){
            for(int i = 0; i < zxList.size(); i++){
                if(i == 0){
                    continue;
                }
                JSONObject zx = new JSONObject();
                zx.put("id", zxList.get(i).getId());
                zx.put("title", zxList.get(i).getTitle());
                zx.put("date_created", DateUtils.format(zxList.get(i).getDateCreated(), DateUtils.DATE_FORMAT1));
                zxArray.add(zx);
            }
        }

        if(Preconditions.isNotBlank(kchdList)){
            for(int i = 0; i < kchdList.size(); i++){
                JSONObject kchd = new JSONObject();
                kchd.put("id", kchdList.get(i).getId());
                kchd.put("title", kchdList.get(i).getTitle());
                kchd.put("date_created", DateUtils.format(kchdList.get(i).getDateCreated(), DateUtils.DATE_FORMAT1));
                kchdArray.add(kchd);
            }
        }
        result.put("ZX", zxArray);
        result.put("KCHD", kchdArray);
        return ResponseContent.buildSuccess(null, result);
    }


    @RequestMapping("/v1/getLastestInformation")
    @ResponseBody
    public ResponseContent<JSONObject> getLastestInformation() {
        Information information = informationService.getLastestInformationByType(InformationType.ZX);
        JSONObject result = new JSONObject();
        if(Preconditions.isNotBlank(information)){
            result.put("id", information.getId());
            result.put("title", information.getTitle());
            result.put("image_url", String.format(imageUrl, information.getImageStore().getId()));
            result.put("describe", information.getDescribe());
            result.put("date_created", DateUtils.format(information.getDateCreated(), DateUtils.DATE_FORMAT1));
        }

        return ResponseContent.buildSuccess(null, result);
    }

    @RequestMapping("/v1/getInformationDetailById/{id}")
    @ResponseBody
    public ResponseContent<JSONObject> getLastestInformation(@PathVariable Long id) {
        Information information = informationService.findById(id);
        JSONObject jsonObject = new JSONObject();
        if(Preconditions.isNotBlank(information)){
            jsonObject.put("id", information.getId());
            jsonObject.put("title", information.getTitle());
            jsonObject.put("information_detail", information.getInformationDetail());
            jsonObject.put("date_created", DateUtils.format(information.getDateCreated(), DateUtils.DATE_FORMAT1));
        }
        return ResponseContent.buildSuccess(null, jsonObject);
    }

}